package core;
import java.util.*;

public class SearchPath {
        @SuppressWarnings("unchecked")
		public static Path getPath(final FlowGraph flowGraph, final Vertex from, final Vertex to, final Path path, final double delta) {
                VertexData vertexData = flowGraph.getVerticesMap().get(from);
                vertexData.setVisited(true);
                
                Iterator<Edge> edgesIterator = flowGraph.getGraph().incidentEdges(from);
                while(edgesIterator.hasNext()) {
                	Edge edge = edgesIterator.next();
                        Vertex vertex = flowGraph.getGraph().opposite(from, edge);
                        
                        if (!flowGraph.getVerticesMap().get(vertex).isVisited() && flowGraph.getEdgesMap().get(edge).getCapacity() > delta && (vertex == edge.getSecondEndpoint() || vertex == edge.getFirstEndpoint())) {
                                if (vertex == to){
                                        path.add(edge);
                                        return path;
                                }

                                path.add(edge);
                                Path innerPath = getPath(flowGraph, vertex, to, path, delta);
                                if (innerPath.getEdges().getLast().getSecondEndpoint() == to) {
                                        return innerPath;
                                }
                                path.getEdges().removeLast();
                        }
                        flowGraph.getVerticesMap().get(from).setVisited(true);
                }
                return path;
        }
}